package com.situ.staff.score.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.staff.score.model.ScoreModel;
import com.situ.staff.score.service.Impl.ScoreServiceImpl;
import com.situ.staff.util.FmtRequest;

/**
 * Servlet implementation class ScoreServlet
 */
@WebServlet("/ScoreServlet")
public class ScoreServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       ScoreServiceImpl service = new ScoreServiceImpl();

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		Object res ="";
		switch (action) {
		case "page":
			try {
				res = page(request);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		case "add":
			try {
				res = add(request);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		case "del":
			try {
				res = del(request);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		case "get":
			try {
				res = get(request);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		case "upd":
			try {
				res = upd(request);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		default:
			break;
		}
		FmtRequest.write(response.getWriter(), res);
	}

	private Object upd(HttpServletRequest request) throws SQLException {
		ScoreModel model = FmtRequest.parseModel(request, ScoreModel.class);
		System.out.println(model.toString());
		return service.update(model);
	}

	private Object get(HttpServletRequest request) throws SQLException {
		ScoreModel model = FmtRequest.parseModel(request, ScoreModel.class);
//		System.out.println(model.toString());
//		System.out.println(model==null);
		return service.selectModel(model);
	}

	private Object del(HttpServletRequest request) throws SQLException {
		ScoreModel model = FmtRequest.parseModel(request, ScoreModel.class);
		return service.delete(model);
	}

	private Object add(HttpServletRequest request) throws SQLException {
		ScoreModel model = FmtRequest.parseModel(request, ScoreModel.class);
		return service.insert(model);
	}

	private Object page(HttpServletRequest request) throws SQLException {
		Map<String, String> fields = new HashMap<String, String>();
		fields.put("codeEmp","codeEmp");
		fields.put("codePro","codePro");
		ScoreModel model = FmtRequest.parseModel(request, ScoreModel.class,fields);
		String pageIndex = request.getParameter("pageIndex");
		String pageLimit = request.getParameter("pageLimit");
		model.setPageIndex(Integer.parseInt(pageIndex));
		model.setPageLimit(Integer.parseInt(pageLimit));
		model.setPageOn(true);
//		System.out.println(model);
		List<ScoreModel> list = service.selectList(model);
//		System.out.println(list.toString());
		Integer count = service.selectCount(model);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list",list);
		map.put("count", count);
		
		return map;
	}

}
